package com.ebt.m.apps;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.apps.response.ModulesBean;
import com.ebt.m.customer.view.BadgeView;
import com.ebt.m.utils.DataValidation;
import com.ebt.m.utils.imageloader.ImageMgr;
import com.ebt.m.commons.buscomponent.listview.IView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Damon on 2017/10/26.
 */

public class AppsViewItem extends IView implements View.OnClickListener {
    ModulesBean modulesBean;
    @BindView(R.id.img)
    ImageView img;
    @BindView(R.id.txt)
    TextView txt;
    @BindView(R.id.badge_view)
    BadgeView badgeView;

    public AppsViewItem(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        inflate(context, R.layout.frg_apps_item, this);
    }

    public AppsViewItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AppsViewItem(Context context) {
        this(context, null);
        ButterKnife.bind(this);
        setOnClickListener(this);
    }

    @Override
    public void update(Object... args) {
        modulesBean = (ModulesBean) args[0];
        if (DataValidation.isEmpty(modulesBean.getIconUrl())) {
            ImageMgr.getInstance().displayImage(img, modulesBean.getIconId(), "");
        } else {
            ImageMgr.getInstance().displayImage(img, modulesBean.getIconUrl(), "");
        }
        txt.setText(modulesBean.getModuleName());
        int count = modulesBean.getCount();
        if (count > 0) {
            badgeView.setVisibility(VISIBLE);
            badgeView.setText(count + "");
        } else {
            badgeView.setVisibility(INVISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnclickListner != null) {
            mOnclickListner.onClick(v, modulesBean);
        }
    }
}
